//
//  PPResourceManager.m
//  pickpic
//
//  Created by Jinxiao on 9/16/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPResourceManager.h"

#import "PPSocializationManager.h"

#import "PPAccountManager.h"

#import "PPAlbumManager.h"

#import "PPAlbumTagsManager.h"

#import "PPUnreadMessageManager.h"
#import "PPFriendsManager.h"
#import "PPActivityCommitManager.h"

@interface PPResourceManager ()

@end

@implementation PPResourceManager

+ (instancetype)sharedInstance {
    static id instance;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        instance = self.new;
    });

    return instance;
}

- (instancetype)init {
    self = [super init];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveApplicationEnterBackgroundNotification) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveApplicationEnterForegroundNotification) name:UIApplicationWillEnterForegroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveAccountChangedNotification) name:DGCurrentAccountDidChangeNotification object:nil];

    return self;
}

- (void)didReceiveAccountChangedNotification {
    [[PPAccountManager sharedInstance] updateProfilesWithCompletion:nil];

    [[PPAlbumManager sharedInstance] synchronize];

    [[PPUnreadMessageManager sharedInstance] fetchUnreadMessages];

    [[PPActivityCommitManager sharedInstance] commitIfNeeded];

    [[PPFriendsManager sharedInstance] fetchAllFriendsWithCallback:nil];
}

- (void)didReceiveApplicationEnterBackgroundNotification {

}

- (void)didReceiveApplicationEnterForegroundNotification {
    [[PPUnreadMessageManager sharedInstance] fetchUnreadMessages];

    [[PPActivityCommitManager sharedInstance] commitIfNeeded];
}

- (void)launchWithOptions:(NSDictionary *)options {
    [[PPAccountManager sharedInstance] updateProfilesWithCompletion:nil];

    [PPSocializationManager sharedInstance];

    [[PPAlbumTagsManager sharedInstance] fetchHotTags];
    [[PPAlbumTagsManager sharedInstance] fetchCustomTags];

    [[PPAlbumManager sharedInstance] synchronize];

    [[PPUnreadMessageManager sharedInstance] fetchUnreadMessages];

    [[PPActivityCommitManager sharedInstance] commitIfNeeded];

    [[PPFriendsManager sharedInstance] fetchAllFriendsWithCallback:nil];
}

@end
